package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.Exame;
import com.model.TipoAlimentacao;

import com.model.RegAlimentacoes;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table REG_ALIMENTACOES.
*/
public class RegAlimentacoesDao extends AbstractDao<RegAlimentacoes, Long> {

    public static final String TABLENAME = "REG_ALIMENTACOES";

    /**
     * Properties of entity RegAlimentacoes.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idAlimentacao");
        public final static Property NomeEnxame = new Property(1, String.class, "nomeEnxame", false, "NOME_ENXAME");
        public final static Property Ano = new Property(2, String.class, "Ano", false, "ANO");
        public final static Property Mes = new Property(3, String.class, "mes", false, "MES");
        public final static Property Data = new Property(4, String.class, "data", false, "DATA");
        public final static Property TipoAlimento = new Property(5, Integer.class, "tipoAlimento", false, "TIPO_ALIMENTO");
        public final static Property Quantidade = new Property(6, Float.class, "quantidade", false, "QUANTIDADE");
        public final static Property CustoEstimado = new Property(7, Integer.class, "custoEstimado", false, "CUSTO_ESTIMADO");
        public final static Property Objectivo = new Property(8, String.class, "objectivo", false, "OBJECTIVO");
        public final static Property Diasproxali = new Property(9, Integer.class, "diasproxali", false, "DIASPROXALI");
        public final static Property Isnote = new Property(10, Boolean.class, "isnote", false, "ISNOTE");
        public final static Property CodExame = new Property(11, long.class, "codExame", false, "COD_EXAME");
        public final static Property CodApairio = new Property(12, long.class, "codApairio", false, "COD_APAIRIO");
        public final static Property CodApicultor = new Property(13, long.class, "codApicultor", false, "COD_APICULTOR");
        public final static Property TipoAlimenta = new Property(14, long.class, "tipoAlimenta", false, "TIPO_ALIMENTA");
    };

    private DaoSession daoSession;

    private Query<RegAlimentacoes> tipoAlimentacao_ApiarioTipoAlimentaQuery;
    private Query<RegAlimentacoes> apiario_ApiarioRegAlimentacoesQuery;
    private Query<RegAlimentacoes> apicultor_ApicultorRegAlimentacoesQuery;
    private Query<RegAlimentacoes> exame_ExameRegAlimentacoesQuery;

    public RegAlimentacoesDao(DaoConfig config) {
        super(config);
    }
    
    public RegAlimentacoesDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'REG_ALIMENTACOES' (" + //
                "'_idAlimentacao' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'NOME_ENXAME' TEXT," + // 1: nomeEnxame
                "'ANO' TEXT," + // 2: Ano
                "'MES' TEXT," + // 3: mes
                "'DATA' TEXT," + // 4: data
                "'TIPO_ALIMENTO' INTEGER," + // 5: tipoAlimento
                "'QUANTIDADE' REAL," + // 6: quantidade
                "'CUSTO_ESTIMADO' INTEGER," + // 7: custoEstimado
                "'OBJECTIVO' TEXT," + // 8: objectivo
                "'DIASPROXALI' INTEGER," + // 9: diasproxali
                "'ISNOTE' INTEGER," + // 10: isnote
                "'COD_EXAME' INTEGER NOT NULL ," + // 11: codExame
                "'COD_APAIRIO' INTEGER NOT NULL ," + // 12: codApairio
                "'COD_APICULTOR' INTEGER NOT NULL ," + // 13: codApicultor
                "'TIPO_ALIMENTA' INTEGER NOT NULL );"); // 14: tipoAlimenta
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'REG_ALIMENTACOES'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, RegAlimentacoes entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String nomeEnxame = entity.getNomeEnxame();
        if (nomeEnxame != null) {
            stmt.bindString(2, nomeEnxame);
        }
 
        String Ano = entity.getAno();
        if (Ano != null) {
            stmt.bindString(3, Ano);
        }
 
        String mes = entity.getMes();
        if (mes != null) {
            stmt.bindString(4, mes);
        }
 
        String data = entity.getData();
        if (data != null) {
            stmt.bindString(5, data);
        }
 
        Integer tipoAlimento = entity.getTipoAlimento();
        if (tipoAlimento != null) {
            stmt.bindLong(6, tipoAlimento);
        }
 
        Float quantidade = entity.getQuantidade();
        if (quantidade != null) {
            stmt.bindDouble(7, quantidade);
        }
 
        Integer custoEstimado = entity.getCustoEstimado();
        if (custoEstimado != null) {
            stmt.bindLong(8, custoEstimado);
        }
 
        String objectivo = entity.getObjectivo();
        if (objectivo != null) {
            stmt.bindString(9, objectivo);
        }
 
        Integer diasproxali = entity.getDiasproxali();
        if (diasproxali != null) {
            stmt.bindLong(10, diasproxali);
        }
 
        Boolean isnote = entity.getIsnote();
        if (isnote != null) {
            stmt.bindLong(11, isnote ? 1l: 0l);
        }
        stmt.bindLong(12, entity.getCodExame());
        stmt.bindLong(13, entity.getCodApairio());
        stmt.bindLong(14, entity.getCodApicultor());
        stmt.bindLong(15, entity.getTipoAlimenta());
    }

    @Override
    protected void attachEntity(RegAlimentacoes entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public RegAlimentacoes readEntity(Cursor cursor, int offset) {
        RegAlimentacoes entity = new RegAlimentacoes( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // nomeEnxame
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // Ano
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // mes
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // data
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // tipoAlimento
            cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6), // quantidade
            cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7), // custoEstimado
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // objectivo
            cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9), // diasproxali
            cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0, // isnote
            cursor.getLong(offset + 11), // codExame
            cursor.getLong(offset + 12), // codApairio
            cursor.getLong(offset + 13), // codApicultor
            cursor.getLong(offset + 14) // tipoAlimenta
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, RegAlimentacoes entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setNomeEnxame(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setAno(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setMes(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setData(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setTipoAlimento(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setQuantidade(cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6));
        entity.setCustoEstimado(cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7));
        entity.setObjectivo(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setDiasproxali(cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9));
        entity.setIsnote(cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0);
        entity.setCodExame(cursor.getLong(offset + 11));
        entity.setCodApairio(cursor.getLong(offset + 12));
        entity.setCodApicultor(cursor.getLong(offset + 13));
        entity.setTipoAlimenta(cursor.getLong(offset + 14));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(RegAlimentacoes entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(RegAlimentacoes entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioTipoAlimenta" to-many relationship of TipoAlimentacao. */
    public List<RegAlimentacoes> _queryTipoAlimentacao_ApiarioTipoAlimenta(long tipoAlimenta) {
        synchronized (this) {
            if (tipoAlimentacao_ApiarioTipoAlimentaQuery == null) {
                QueryBuilder<RegAlimentacoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.TipoAlimenta.eq(null));
                tipoAlimentacao_ApiarioTipoAlimentaQuery = queryBuilder.build();
            }
        }
        Query<RegAlimentacoes> query = tipoAlimentacao_ApiarioTipoAlimentaQuery.forCurrentThread();
        query.setParameter(0, tipoAlimenta);
        return query.list();
    }

    /** Internal query to resolve the "apiarioRegAlimentacoes" to-many relationship of Apiario. */
    public List<RegAlimentacoes> _queryApiario_ApiarioRegAlimentacoes(long codApairio) {
        synchronized (this) {
            if (apiario_ApiarioRegAlimentacoesQuery == null) {
                QueryBuilder<RegAlimentacoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApairio.eq(null));
                apiario_ApiarioRegAlimentacoesQuery = queryBuilder.build();
            }
        }
        Query<RegAlimentacoes> query = apiario_ApiarioRegAlimentacoesQuery.forCurrentThread();
        query.setParameter(0, codApairio);
        return query.list();
    }

    /** Internal query to resolve the "apicultorRegAlimentacoes" to-many relationship of Apicultor. */
    public List<RegAlimentacoes> _queryApicultor_ApicultorRegAlimentacoes(long codApicultor) {
        synchronized (this) {
            if (apicultor_ApicultorRegAlimentacoesQuery == null) {
                QueryBuilder<RegAlimentacoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApicultor.eq(null));
                apicultor_ApicultorRegAlimentacoesQuery = queryBuilder.build();
            }
        }
        Query<RegAlimentacoes> query = apicultor_ApicultorRegAlimentacoesQuery.forCurrentThread();
        query.setParameter(0, codApicultor);
        return query.list();
    }

    /** Internal query to resolve the "exameRegAlimentacoes" to-many relationship of Exame. */
    public List<RegAlimentacoes> _queryExame_ExameRegAlimentacoes(long codExame) {
        synchronized (this) {
            if (exame_ExameRegAlimentacoesQuery == null) {
                QueryBuilder<RegAlimentacoes> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodExame.eq(null));
                exame_ExameRegAlimentacoesQuery = queryBuilder.build();
            }
        }
        Query<RegAlimentacoes> query = exame_ExameRegAlimentacoesQuery.forCurrentThread();
        query.setParameter(0, codExame);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getTipoAlimentacaoDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T3", daoSession.getExameDao().getAllColumns());
            builder.append(" FROM REG_ALIMENTACOES T");
            builder.append(" LEFT JOIN TIPO_ALIMENTACAO T0 ON T.'TIPO_ALIMENTA'=T0.'_idTipoAlimentacao'");
            builder.append(" LEFT JOIN APIARIO T1 ON T.'COD_APAIRIO'=T1.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T2 ON T.'COD_APICULTOR'=T2.'_idApicultor'");
            builder.append(" LEFT JOIN EXAME T3 ON T.'COD_EXAME'=T3.'_idExame'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected RegAlimentacoes loadCurrentDeep(Cursor cursor, boolean lock) {
        RegAlimentacoes entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        TipoAlimentacao tipoAlimentacao = loadCurrentOther(daoSession.getTipoAlimentacaoDao(), cursor, offset);
         if(tipoAlimentacao != null) {
            entity.setTipoAlimentacao(tipoAlimentacao);
        }
        offset += daoSession.getTipoAlimentacaoDao().getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }

        return entity;    
    }

    public RegAlimentacoes loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<RegAlimentacoes> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<RegAlimentacoes> list = new ArrayList<RegAlimentacoes>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<RegAlimentacoes> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<RegAlimentacoes> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
